package com.myway.dto.util;

import com.myway.dto.Result.ArrivalDate;
import com.myway.dto.Result.ArrivalTime;
import com.myway.dto.Result.DepartureDate;
import com.myway.dto.Result.DepartureTime;
import com.myway.dto.ibe_param.DepatureDateParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class DateUtils{

    /**
     * 时间加一天
     * @param date
     * @return
     */
    public static  Date addOneDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, 1); // 日期加1天
        date= c.getTime();
        return date;
    }

    /**
     * 计算当前时间的前几天和后几天
     * @param date
     * @param a
     * @return
     */
    public static String addjianDay(String date,int a){
        try {
            Date parse = null;
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            parse = df.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DATE,a);
            Date time = c.getTime();
            String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
            return  format;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**

     * @Deprecated 计算两天相差几天
     * @return
     */
    public static int numDays(String day1, String day2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = format.parse(day1);
            Date date2 = format.parse(day2);
            return (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
        } catch (ParseException e) {
            return 0;
        }
    }


    public static DepartureDate getDate(String date){
        DepartureDate da = new DepartureDate();
        String[] split = date.split("-");
        da.setYear(Integer.parseInt(split[0]));
        da.setMonth(Integer.parseInt(split[1]));
        da.setDay(Integer.parseInt(split[2]));
        return da;
    }

    public static DepatureDateParam getDateParam(String date){
        DepatureDateParam da = new DepatureDateParam();
        String[] split = date.split("-");
        da.setYear(Integer.parseInt(split[0]));
        da.setMonth(Integer.parseInt(split[1]));
        da.setDay(Integer.parseInt(split[2]));
        return da;
    }

    public static String getDepart(DepartureDate data,DepartureTime time){
        Integer mon = data.getMonth();
        Integer day = data.getDay();
        Integer hour = time.getHour();
        Integer minute = time.getMinutes();
        String month = mon.toString().length() == 1 ? "0" + mon.toString() : mon.toString();
        String days = day.toString().length() == 1 ? "0" + day.toString() : day.toString();
        String hours=hour.toString().length()==1? "0" + hour.toString() : hour.toString();
        String minutes=minute.toString().length()==1? "0" + minute.toString() : minute.toString();
        return data.getYear()+"-"+month+"-"+days+"T"+hours+":"+minutes+":00";
    }

    public static String getArriva(ArrivalDate data,ArrivalTime time){
        Integer mon = data.getMonth();
        Integer day = data.getDay();
        Integer hour = time.getHour();
        Integer minute = time.getMinutes();
        String month = mon.toString().length() == 1 ? "0" + mon.toString() : mon.toString();
        String days = day.toString().length() == 1 ? "0" + day.toString() : day.toString();
        String hours=hour.toString().length()==1? "0" + hour.toString() : hour.toString();
        String minutes=minute.toString().length()==1? "0" + minute.toString() : minute.toString();
        return data.getYear()+"-"+month+"-"+days+"T"+hours+":"+minutes+":00";
    }

    public static String getDepartTime(DepartureTime data){
        if(data.getMinutes() < 10 && data.getMinutes() >= 0){
            return data.getHour()+":0"+data.getMinutes();
        }
        return data.getHour()+":"+data.getMinutes();
    }

    public static String getArrivalTime(ArrivalTime data){
        if(data.getMinutes() < 10 && data.getMinutes() >= 0){
            return data.getHour()+":0"+data.getMinutes();
        }
        return  data.getHour()+":"+data.getMinutes();
    }


    public static String getDepartureDateStr(DepartureDate departureDate){
        Integer mon = departureDate.getMonth();
        Integer day = departureDate.getDay();
        String month = mon.toString().length() == 1 ? "0" + mon.toString() : mon.toString();
        String days = day.toString().length() == 1 ? "0" + day.toString() : day.toString();
        return departureDate.getYear()+"-"+month+"-"+days;
    }

    /**
     *@param date
     *@return
     *
     *@author huang_yu
     *@description
     *@date 2008-9-20
     */
    public static Date stringToDate(String date) {
        if (date == null)
            return null;

        Calendar cd = Calendar.getInstance();
        StringTokenizer token = new StringTokenizer(date, "-/ :");
        if (token.hasMoreTokens()) {
            cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.YEAR, 1970);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
        } else {
            cd.set(Calendar.MONTH, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.DAY_OF_MONTH, 1);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.HOUR_OF_DAY, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.MINUTE, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.SECOND, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.MILLISECOND, 0);
        }

        return cd.getTime();
    }

}
